package com.siwei.mes.service.system.impl;

import com.siwei.mes.entity.laboratory.sync.SyncLaboratoryMaterialsInfo;
import com.siwei.mes.entity.materials.MaterialsSpecConfig;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.laboratory.SyncLaboratoryMapper;
import com.siwei.mes.mapper.materials.MaterialsSpecConfigMapper;
import com.siwei.mes.service.system.InitializationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 初始化相关
 *
 * @Description: 初始化相关
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/4/24 22:12
 */
@Service
@Slf4j
public class InitializationServiceImpl implements InitializationService {
    @Resource
    private SyncLaboratoryMapper syncLaboratoryMapper;
    @Resource
    private MaterialsSpecConfigMapper materialsSpecConfigMapper;

    @Override
    public List<MaterialsSpecConfig> initMaterials(String itemId, Integer type) {

        List<MaterialsSpecConfig> allList = new ArrayList<>();

        boolean isAppendGradeName = this.isAppendGradeName(type);

        /**如果这里是混凝土的类型 这里手动赋值*/
        if (type.equals(MaterialsEnum.CONCRETE.getCode())) {
            List<String> tpzList = getTpzList();

            for (String tpz : tpzList) {
                MaterialsSpecConfig specConfig = new MaterialsSpecConfig();
                specConfig.setMaterialsType(type);
                specConfig.setMaterialsName("混凝土");
                specConfig.setMaterialAbbreviation(null);
                specConfig.setMaterialsSpec(tpz);
                specConfig.setIsLaboratory(0);

                MaterialsSpecConfig select = materialsSpecConfigMapper.selectOne(new MaterialsSpecConfig()
                        .setMaterialsName("混凝土").setMaterialsSpec(tpz).setMaterialsType(type));
                if (select == null) {
                    specConfig.init(true);
                    materialsSpecConfigMapper.insertSelective(specConfig);
                } else {
                    specConfig.init(false);
                    specConfig.setId(select.getId());
                    materialsSpecConfigMapper.updateByPrimaryKeySelective(specConfig);
                }
                allList.add(specConfig);
            }
            return allList;
        }
        List<String> itemIdList = Arrays.asList(itemId.split(","));

        List<SyncLaboratoryMaterialsInfo> itemSampleMapList = syncLaboratoryMapper.selectItemSample(itemIdList, type);
        if (itemSampleMapList.isEmpty()) {
            return allList;
        }

        for (SyncLaboratoryMaterialsInfo syncInfo : itemSampleMapList) {
            MaterialsSpecConfig materialsSpecConfig = SyncLaboratoryMaterialsInfo.payloadMaterials(syncInfo, isAppendGradeName);

            /**这里先判断有没有 有就是修改 没有就新增*/
            MaterialsSpecConfig selectInfo = new MaterialsSpecConfig().setIsLaboratory(1)
                    .setMaterialsName(materialsSpecConfig.getMaterialsName())
                    .setMaterialsSpec(materialsSpecConfig.getMaterialsSpec())
                    .setMaterialsType(materialsSpecConfig.getMaterialsType())
                    .setSampleId(materialsSpecConfig.getSampleId());

            MaterialsSpecConfig select = materialsSpecConfigMapper.selectOne(selectInfo);
            if (select == null) {
                materialsSpecConfig.init(true);
                materialsSpecConfigMapper.insertSelective(materialsSpecConfig);
            } else {
                materialsSpecConfig.init(false);
                materialsSpecConfig.setId(select.getId());
                materialsSpecConfigMapper.updateByPrimaryKeySelective(materialsSpecConfig);
            }
            allList.add(materialsSpecConfig);
        }
        return allList;
    }

    /**
     * 判断是否需要拼接gradeName
     *
     * @param type 1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
     * @return true-需要   false-不需要
     */
    private boolean isAppendGradeName(Integer type) {
        List<Integer> notAppendList = new ArrayList<>();
        notAppendList.add(MaterialsEnum.FINE.getCode());
        return !notAppendList.contains(type);
    }

    /**
     * 获取C5到C100  隔5递增
     *
     * @return
     */
    public static List<String> getTpzList() {
        List<String> tpzList = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            if (i % 5 == 0) { // 检查i是否能被5整除
                tpzList.add("C" + i);
            }
        }

        /**这里添加水下  C20-C60*/
        for (int i = 20; i <= 60; i++) {
            if (i % 5 == 0) { // 检查i是否能被5整除
                tpzList.add("水下C" + i);
            }
        }
        return tpzList;
    }

    public static void main(String[] args) {
        System.out.println(getTpzList());
    }
}
